import 'package:flutter/material.dart';

import 'package:industry_net/base/common/util.dart';

import 'controller.dart';
import 'state.dart';

class NewsCommentBar extends StatefulWidget {
  const NewsCommentBar({Key? key, required this.controller}) : super(key: key);
  final CommentController controller;
  @override
  _NewsCommentBarState createState() => _NewsCommentBarState();
}

class _NewsCommentBarState extends State<NewsCommentBar> {
  late CommentController controller;
  late CommentState state;

  @override
  void initState() {
    super.initState();
    controller = widget.controller;
    state = controller.state;
    controller.update = () {
      setState(() {});
    };
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      constraints: BoxConstraints(minHeight: 50.r),
      padding: EdgeInsets.fromLTRB(gap12, 0, 0, 0),
      decoration: const BoxDecoration(
        color: ColorsUtil.white,
        // border: Border(top: BorderSide(color: ColorsUtil.separator,width: 1))
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            color: ColorsUtil.separator,
            height: 1,
            margin: EdgeInsets.only(bottom: 10.r),
          ),
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Expanded(
                child: SizedBox(
                  height:
                      state.focusNode.hasFocus || state.isEditing ? null : 32.r,
                  child: TextField(
                    controller: state.editCtrl,
                    focusNode: state.focusNode,
                    toolbarOptions: const ToolbarOptions(
                        copy: true, cut: true, paste: true, selectAll: true),
                    maxLines: 4,
                    minLines: 1,
                    decoration: InputDecoration(
                      fillColor: ColorsUtil.white,
                      filled: true,
                      hintText:
                          state.focusNode.hasFocus ? state.hintText : '说两句',
                      hintStyle: pfMediumStyle(
                          color: ColorsUtil.placeholder, fontSize: font13),
                      labelStyle: pfMediumStyle(
                          color: ColorsUtil.title, fontSize: font13),
                      prefixIcon: state.focusNode.hasFocus || state.isEditing
                          ? null
                          : Icon(
                              IconUtil.edit,
                              color: ColorsUtil.placeholder,
                              size: 14.r,
                            ),
                      enabledBorder: OutlineInputBorder(
                          borderRadius: state.isEditing
                              ? BorderRadius.circular(circle4)
                              : BorderRadius.circular(16.r),
                          borderSide: BorderSide(
                              color: const Color(0xffdddddd), width: 0.5.r)),
                      focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(circle4),
                          borderSide: BorderSide(
                              color: const Color(0xffdddddd), width: 0.5.r)),
                      contentPadding:
                          EdgeInsets.symmetric(horizontal: 10.r, vertical: 8.r),
                    ),
                  ),
                ),
              ),
              Visibility(
                visible: !state.isEditing,
                child: _createIconBtn(
                    count: '215',
                    callback: controller.actionPraise,
                    iconData: IconUtil.praise),
              ),
              Visibility(
                visible: !state.isEditing,
                child: _createIconBtn(
                    count: '96',
                    callback: controller.actionCollect,
                    url: 'icon_collect.png'.imgPathNews),
              ),
              Visibility(
                visible: !state.isEditing,
                child: _createIconBtn(
                    callback: controller.actionCollect,
                    url: 'icon_forward.png'.imgPathNews),
              ),
              Visibility(
                visible: state.isEditing,
                child: InkWell(
                  onTap: controller.actionCollect,
                  child: Container(
                    margin: EdgeInsets.only(right: gap12, left: gap10),
                    padding:
                        EdgeInsets.symmetric(horizontal: 10.r, vertical: 5.r),
                    decoration: BoxDecoration(
                        color: ColorsUtil.green,
                        borderRadius: BorderRadius.circular(4.r)),
                    child: Text(
                      '发送',
                      style: pfMediumStyle(
                          color: ColorsUtil.white, fontSize: font12),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

// class NewsCommentBar extends StatelessWidget {
//   NewsCommentBar({Key? key}) : super(key: key);
//   final controller = CommentController();
//   final state = Get.find<CommentController>().state;
//   @override
//   Widget build(BuildContext context) {
//     return GetBuilder<CommentController>(
//       builder: (_) => Container(
//         width: double.infinity,
//         constraints: BoxConstraints(minHeight: 50.r),
//         padding: EdgeInsets.fromLTRB(gap12, 0, 0, 0),
//         decoration: const BoxDecoration(
//           color: ColorsUtil.white,
//         ),
//         child: Column(
//           mainAxisSize: MainAxisSize.min,
//           children: [
//             Container(
//               color: ColorsUtil.separator,
//               height: 1,
//               margin: EdgeInsets.only(bottom: 10.r),
//             ),
//             Row(
//               mainAxisSize: MainAxisSize.min,
//               children: [
//                 Expanded(
//                   child: SizedBox(
//                     height: state.focusNode.hasFocus || state.isEditing
//                         ? null
//                         : 32.r,
//                     child: TextField(
//                       controller: state.editCtrl,
//                       // focusNode: state.focusNode,
//                       toolbarOptions: const ToolbarOptions(
//                           copy: true, cut: true, paste: true, selectAll: true),
//                       maxLines: 4,
//                       minLines: 1,
//                       decoration: InputDecoration(
//                         fillColor: ColorsUtil.white,
//                         filled: true,
//                         hintText: '说两句',
//                         hintStyle: pfMediumStyle(
//                             color: ColorsUtil.placeholder, fontSize: font13),
//                         labelStyle: pfMediumStyle(
//                             color: ColorsUtil.title, fontSize: font13),
//                         prefixIcon: state.focusNode.hasFocus || state.isEditing
//                             ? null
//                             : Icon(
//                                 IconUtil.edit,
//                                 color: ColorsUtil.placeholder,
//                                 size: 14.r,
//                               ),
//                         enabledBorder: OutlineInputBorder(
//                             borderRadius: state.isEditing
//                                 ? BorderRadius.circular(circle4)
//                                 : BorderRadius.circular(16.r),
//                             borderSide: BorderSide(
//                                 color: const Color(0xffdddddd), width: 0.5.r)),
//                         focusedBorder: OutlineInputBorder(
//                             borderRadius: BorderRadius.circular(circle4),
//                             borderSide: BorderSide(
//                                 color: const Color(0xffdddddd), width: 0.5.r)),
//                         contentPadding: EdgeInsets.symmetric(
//                             horizontal: 10.r, vertical: 8.r),
//                       ),
//                     ),
//                   ),
//                 ),
//                 Visibility(
//                   visible: !state.isEditing,
//                   child: _createIconBtn(
//                       count: '215',
//                       callback: controller.actionPraise,
//                       iconData: IconUtil.praise),
//                 ),
//                 Visibility(
//                   visible: !state.isEditing,
//                   child: _createIconBtn(
//                       count: '96',
//                       callback: controller.actionCollect,
//                       url: 'icon_collect.png'.imgPathNews),
//                 ),
//                 Visibility(
//                   visible: !state.isEditing,
//                   child: _createIconBtn(
//                       callback: controller.actionCollect,
//                       url: 'icon_forward.png'.imgPathNews),
//                 ),
//                 Visibility(
//                   visible: state.isEditing,
//                   child: InkWell(
//                     onTap: controller.actionCollect,
//                     child: Container(
//                       margin: EdgeInsets.only(right: gap12, left: gap10),
//                       padding:
//                           EdgeInsets.symmetric(horizontal: 10.r, vertical: 5.r),
//                       decoration: BoxDecoration(
//                           color: ColorsUtil.green,
//                           borderRadius: BorderRadius.circular(4.r)),
//                       child: Text(
//                         '发送',
//                         style: pfMediumStyle(
//                             color: ColorsUtil.white, fontSize: font12),
//                       ),
//                     ),
//                   ),
//                 ),
//               ],
//             ),
//           ],
//         ),
//       ),
//     );
//   }

Widget _createIconBtn(
    {IconData? iconData, String? url, String? count, VoidCallback? callback}) {
  return InkWell(
    onTap: callback,
    child: Container(
      padding: EdgeInsets.symmetric(horizontal: 5.r),
      constraints: BoxConstraints(minWidth: 40.r, maxWidth: 60.r),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          iconData != null
              ? Icon(
                  iconData,
                  color: const Color(0xff333333),
                  size: 20.r,
                )
              : loadImage(
                  url: url, width: 23.r, height: 23.r, fit: BoxFit.contain),
          Text(
            count ?? '',
            style: pfMediumStyle(color: ColorsUtil.title, fontSize: font10),
          ),
        ],
      ),
    ),
  );
}
